// 发送请求获取数据
//点击轮播图 预览大图
import {
    request
} from '../../request/index'
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsObj: {},
        //商品是否被收藏
        isCollect: false,
    },
    goodsInfo: {},

    // onLoad: function (options) {
    //     const {goods_id}=options;
    //     console.log(goods_id);
    //     this.getGoodsDetail(goods_id)
    // },
    onShow: function () {
        let pages = getCurrentPages();
        let currentPage = pages[pages.length - 1];
        let options = currentPage.options;
        const {
            goods_id
        } = options;
        this.getGoodsDetail(goods_id);

    },
    //获取商品详情数据
    async getGoodsDetail(goods_id) {
        const res = await request({
            url: "/goods/detail",
            data: {
                goods_id
            }
        });
        console.log(res);
        this.goodsInfo = res.data.message;
        //获取缓存中商品收藏的数组
        let collect = wx.getStorageSync('collect') || [];
        //判断当前商品是否被收藏
        let isCollect = collect.some(v => v.goods_id === this.goodsInfo.goods_id)
        this.setData({
            goodsObj: {
                goods_name: res.data.message.goods_name,
                goods_price: res.data.message.goods_price,
                goods_introduce: res.data.message.goods_introduce,
                pics: res.data.message.pics
            },
            isCollect,


        })
    },
    //点击图片，预览大图
    handlePrevewImage(e) {
        const urls = this.goodsInfo.pics.map(v => v.pics_mid);
        const current = e.currentTarget.dataset.url;
        wx.previewImage({
            urls,
            current,
        })
    },
    handleCarAdd() {
        //获取缓存中的购物车 数组
        let cart = wx.getStorageSync('cart') || [];
        //判断商品是否存在于购物车数组中
        let index = cart.findIndex(v => v.goods_id === this.goodsInfo.goods_id);
        if (index === -1) {
            //不存在 第一次添加
            this.goodsInfo.num = 1;
            this.goodsInfo.checked = true;

            cart.push(this.goodsInfo);
        } else {
            //已经存在在购物车数据执行num++
            cart[index].num++;
        }
        //把购物车重新添加回缓存中
        wx.setStorageSync('cart', cart);
        //弹窗提醒
        wx.showToast({
            title: '加入成功',
            icon: "success",
            //ture 防止用户手抖 5秒执行一次
            mask: false,
        })
    },
    //点击商品收藏图标
    handleCollect() {
        let isCollect = false;
        //获取缓存中的商品收藏数组
        let collect = wx.getStorageSync('collect') || [];
        //判断改该商品是否被收藏过
        let index = collect.findIndex(v => v.goods_id === this.goodsInfo.goods_id);
        if (index !== -1) {
            //能找到已经收藏过了，在数组中删除改商品
            collect.splice(index, 1);
            isCollect = false;
            wx.showToast({
                title: '取消成功',
                icon: 'success',
                mask: true
            });
        } else {
            //没有收藏过
            collect.push(this.goodsInfo)
            isCollect = true;
            wx.showToast({
                title: '收藏成功',
                icon: 'success',
                mask: true
            });
        }
        //把数组存入到缓存中
        wx.setStorageSync('collect', collect);
        //修改data中的属性 isCollect
        this.setData({
            isCollect,
        })

    }
})